// pages/editinfo/editinfo.js
const app = getApp()

Page({

  data: {
    change:"",
    ischange:false,
    value:"",
  }, 
  /*
  over:function(){
    if(this.data.change == "编辑"){
      this.setData({
        change: "保存",
        ischange: false
      })
    }
    else{
      this.submit()
      this.setData({
        change: "编辑",
        ischange: true,
      })
    } 
  },
*/
  submit: function (e) {
    var that    = this;
    var phone   = e.detail.value.phone;
    var name    = e.detail.value.username;
    var brand   = e.detail.value.car_brand;
    var model   = e.detail.value.car_model;
    var plate   = e.detail.value.car_number;
    var fdplate = e.detail.value.car_no;
    if(!name)   { wx.showToast({ title: '姓名不能为空'}); return false;}
    if (!phone) { wx.showToast({ title: '电话不能为空'}); return false;}
    if (!brand) { wx.showToast({ title: '汽车品牌不能为空'}); return false;}
    if (!model) { wx.showToast({ title: '汽车型号不能为空'}); return false;}
    if (!plate) { wx.showToast({title: '车牌号不能为空'}); return false;}
    if (!fdplate){ wx.showToast({ title: '发动机号不能为空'}); return false;} 
    if (!(/^1[3|4|5|6|8|7][0-9]\d{4,8}$/.test(phone))) {
      wx.showToast({
        title     : '手机号有误！',
        icon      : 'none',
        duration  : 1500
      })
      return false;
    }else {
      wx.getLocation({
        type: 'gcj02',
        success: function (rs) {
          if (rs){
            wx.request({
              url: app.url + '/user/info_update',
              method: 'post',
              data: {
                'car_brand': brand,
                'car_model': model,
                'car_no': fdplate,
                'car_number': plate,
                'phone': phone,
                'username': name,
                'uid': wx.getStorageSync('uid'),
                longitude: rs.longitude,
                latitude: rs.latitude
              },
              success: function (res) {
                if (res.data.code == 1) {
                  wx.showToast({
                    title: '提交成功',
                    icon: 'success',
                    duration: 1000,
                    success: function () {
                      setTimeout(function () { wx.navigateBack({ delta: 1 }) })
                    }
                  })
                } else {
                  wx.showToast({
                    title: res.data.msg,
                    icon: 'none'
                  })
                }
              }
            })
          }else{
            wx.showToast({
              title: '用户位置获取失败',
              icon: 'none'
            })
          } 
        }
      })
    }
  },
  sure    : function (e) {},
  edit_img: function () {
    var that = this;
    wx.chooseImage({
      count     : 1, 
      sizeType  : ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        wx.showToast({
          title: '上传成功',
        })
        // wx.uploadFile({
        //   url: app.data.Url + 'spell/upload',
        //   filePath: res.tempFilePaths[0],
        //   name: 'image',
        //   header: { "Content-Type": "multipart/form-data" },
        //   success: function (e) {
        //     wx.showToast({
        //       title: '上传成功',
        //     })
        //     wx.hideLoading();
        //   }
        // })
        that.setData({
          src: res.tempFilePaths
        });
        // if (pics.length == 3) {
        //   that.setData({
        //     onload: "none",
        //   })
        // }
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  onLoad: function (options) {
    this.userinfo();
  },
  onReady: function () {
  
  },
  userinfo:function(){
    var that=this;
    wx.request({
      url: app.url + '/user/user_info',
      data: {
        uid: wx.getStorageSync('uid'),
        field: 'username,phone,car_brand,car_model,car_number,car_no',
      },
      method: 'post',
      success: function (res) {
        that.setData(res.data.data)
      }
    })
  },
  onShow: function () {
    
  },
  onHide: function () {
  
  },
  onUnload: function () {
  
  },
  onPullDownRefresh: function () {
  
  },
  onReachBottom: function () {
  
  },
  onShareAppMessage: function () {
  
  },
  
})